#include "apue.h"
#include <setjmp.h>

static void f1(int, int, int, int);
static void f2(void);

static jmp_buf jmpbuffer;
static int globval;

int main(void){
	int autoval;
	register int regval;
	volatile int volaval;
	static int statval;

	globval = 1; autoval = 2; regval = 3; volaval = 4; statval = 5;

	if(setjmp(jmpbuffer) != 0){
		printf("after longjmp:\n");
		printf("globval = %d, autoval = %d, regval = %d, volaval = %d, statval = %d\n", globval, autoval, regval, volaval, statval);
		exit(0);
	}

	/**
	 * change variables after setjmp, but before longjmp
	 */
	globval = 95; autoval = 96; regval = 97; volaval = 98; statval = 99;
	f1(autoval, regval, volaval, statval);
	exit(0);
}

static void f1(int autoval, int regval, int volaval, int statval){
	printf("in f1():\n");
	printf("globval = %d, autoval = %d, regval = %d, volaval = %d, statval = %d\n", globval, autoval, regval, volaval, statval);
	f2();
}

static void f2(){
	longjmp(jmpbuffer, 1);
}
